package com.woniuxy.system.controller;

import com.woniuxy.common.exception.Assert;
import com.woniuxy.common.model.dto.bank.CardBindDto;
import com.woniuxy.common.model.dto.bank.SignatureDto;
import com.woniuxy.common.model.dto.user.LoginDto;
import com.woniuxy.common.model.dto.user.UserRegisterDto;
import com.woniuxy.common.model.vo.user.LoginVo;
import com.woniuxy.common.model.vo.user.UserInfoVo;
import com.woniuxy.common.result.Result;
import com.woniuxy.common.utils.JwtTemplate;
import com.woniuxy.common.utils.TokenUtils;
import com.woniuxy.system.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author yellow docker
 * @since 2025-03-18
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }


    @GetMapping("/hello1/{id}")
    public Result hello1(@PathVariable Integer id){
        if (id < 0){
            Assert.error("id不能为负数...");
        }
        return Result.ok();
    }

    /**
     * @Valid 这个注解是  校验
     *    userRegisterDto 参数中的数据
     *
     * @param userRegisterDto
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody @Valid UserRegisterDto userRegisterDto){

        userService.register(userRegisterDto);

        return Result.ok();
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = userService.login(loginDto);
        return Result.ok(loginVo);
    }

    /**
     * 用户绑卡
     */
    @PostMapping("/bank/card/add")
    public Result<SignatureDto> addBankAccount(
            @RequestBody CardBindDto cardBindDto) {
        SignatureDto signatureDto = userService.addBankAccount(cardBindDto);
        return Result.ok(signatureDto);
    }

    @GetMapping("/test")
    public Result test04() {
        String userId = TokenUtils.getUserIdByToken("userId");
        return Result.ok(userId);
    }

    /**
     * 根据用户id查询
     */
    @ApiOperation("根据用户id查询")
    @GetMapping("/inner/findById/{userId}")
    public Result<UserInfoVo> findById(@PathVariable("userId") Long userId) {
        UserInfoVo userInfoVo = userService.findById(userId);
        return Result.ok(userInfoVo);
    }
}
